//
//  XPAutoFooter.swift
//  PPKit
//
//  Created by xiaopin on 2019/8/21.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import MJRefresh

class XPAutoFooter: MJRefreshAutoFooter {

    var label:UILabel?
    var loadingView:UIActivityIndicatorView?
    
    override func prepare() {
        super.prepare()
        
        self.mj_h = 50

        let label = UILabel(frame: CGRect(x: 0, y: 3, width: pp_screen_width, height: 30))
        label.textColor = pp_color(.CCCCCCC)
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = .center
        
        self.label = label
    
        self.loadingView = UIActivityIndicatorView(style: .gray)
        self.loadingView!.frame = CGRect(x: 0, y: 0, width: pp_screen_width, height: 30)
        
        self.addSubview(self.label!)
        self.addSubview(self.loadingView!)
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        self.loadingView!.center = CGPoint(x: pp_screen_width/2.0, y: self.mj_h*0.5)
    }
    
    override func scrollViewContentOffsetDidChange(_ change: [AnyHashable : Any]!) {
        super.scrollViewContentOffsetDidChange(change)
    }
    
    override func scrollViewContentSizeDidChange(_ change: [AnyHashable : Any]!) {
        super.scrollViewContentSizeDidChange(change)
    }
    
    override func scrollViewPanStateDidChange(_ change: [AnyHashable : Any]!) {
        super.scrollViewPanStateDidChange(change)
    }
    
    override var state: MJRefreshState {
        willSet{
            let oldState = self.state
            if newValue == oldState {return}
            
            switch newValue {
            case .idle:
                self.label?.text = ""
                self.loadingView!.stopAnimating()
            case .refreshing:
                self.label?.text = ""
                self.loadingView!.startAnimating()
            case .noMoreData:
                self.label?.text = "——\("MT_Refresh_NoMoreData".localized)——"
                self.loadingView!.stopAnimating()
            default:
                break
            }
        }
    }
}
